from flask import Flask
from flask import make_response,request
import os

app = Flask(__name__)

localpath='E:/Test/web/myblog'
@app.route("/")
def hello():
    print("=== call route / ")
    basepath = os.path.dirname(__file__)
    filepath = os.path.join(basepath, 'resource', 'mao.jpg')
    html='''
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" >
    <script type="text/javascript" src="js/interactor.js"></script>
    </head>
    <body>
    <h1>Hello World!</h1>
    <img id="canvas" src="resource/mao.jpg" width="300" height="300" />
    </body>
    </html>
    '''
    return html

@app.route("/test")
def hello1():
    return "Hello !"

@app.route('/resource/<string:filename>')
def showImg(filename):
    print("=== call route showImg :"+filename)
    basepath = os.path.dirname(__file__)
    filepath = os.path.join(basepath, 'resource', filename)
    img=open(filepath,'rb').read()
    response=make_response(img)
    response.headers['Content-Type']='image/jpg'
    return response
    
@app.route('/js/<string:filename>')
def showJs(filename):
    print("=== call route showJs :"+filename)
    basepath = os.path.dirname(__file__)
    filepath = os.path.join(basepath, 'js', filename)
    js=open(filepath).read()#.encode('utf-8')
    response=make_response(js)
    print(js)
    response.headers['Content-Type']='application/x-javascript'
    return response
    
if __name__=="__main__":
    print("application debug runing...")
    app.run(debug=True,port=8090)